package com.yuexunit.operationman.nbsy.module.plant.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.adapter.PlantAdapter;
import com.yuexunit.operationman.nbsy.module.plant.base.ICallBack;
import com.yuexunit.operationman.nbsy.module.plant.bean.ImplantMaterialItemListEntity;
import com.yuexunit.operationman.nbsy.module.plant.bean.MaterialBean;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialUpdateModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.impl.MaterialUpdatePresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialUpdateView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.ToastDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/11 0011.
 */

public class Act_Plant_Preview extends KJActivity implements ICallBack, IMaterialUpdateView {
    @Bind(R.id.usr_name)
    TextView usrName;
    @Bind(R.id.usr_num)
    TextView usrNum;
    @Bind(R.id.usr_ops)
    TextView usrOps;
    @Bind(R.id.listview)
    FixedSwipeListView listview;
    @Bind(R.id.all_price)
    TextView allPrice;
    @Bind(R.id.submit)
    TextView submit;

    LoadingDialog loadingDialog;
    MaterialBean materialBean;
    MaterialUpdatePresenter updatePresenter;
    PlantAdapter adapter;
    List<ImplantMaterialItemListEntity> materialItemList = new ArrayList<>();
    ToastDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_plant_preview);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        updatePresenter = new MaterialUpdatePresenter(this, new MaterialUpdateModel());
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getStatus() != null &&
                AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getStatus().equals("1")){
            submit.setVisibility(View.VISIBLE);
        }else{
            submit.setVisibility(View.GONE);
        }
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterialItemList() != null &&
                AppConfig.MATERIALBEAN.getObject().getImplantMaterialItemList().size() > 0){
            materialItemList = AppConfig.MATERIALBEAN.getObject().getImplantMaterialItemList();
        }
        adapter = new PlantAdapter(aty, materialItemList, this, false);
        listview.setAdapter(adapter);

        getPrice();
    }

    private void setTitle()
    {
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("1")) {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    男");
        }else if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("2"))
        {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    女");
        }
        else {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    不详");
        }
        usrNum.setText("住院号" + AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getInHospitalNo());
        usrOps.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getOperationName());
    }

    private void getPrice(){
        float price = 0;
        for(ImplantMaterialItemListEntity materialItem : materialItemList){
            price = price + Float.valueOf(materialItem.getPrice()) * Integer.valueOf(materialItem.getAmount());
        }
        allPrice.setText(price + "");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                dialog =  new ToastDialog(aty).builder();
                dialog.setTxt("确认提交").setCallBack(new ToastDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        dialog.dismiss();
                        materialBean = AppConfig.MATERIALBEAN;
                        materialBean.getObject().getImplantMaterial().setStatus("2");
                        Map<String, String> params = new HashMap<>();
                        params.put("saveData", JSON.toJSONString(materialBean));
                        params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                        params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                        params.put("interType", AppConfig.INTERTYPE);
                        updatePresenter.UpdateMaterial(params);
                    }
                });
                dialog.show();
                break;
        }
    }

    @Override
    public void callBack() {

    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @Override
    public void updateMaterial(SubmitBean submitBean) {
        if(submitBean.getCode() == 200){
            AppConfig.MATERIALBEAN = materialBean;
            ViewInject.toast("更新成功");
            finish();
        }
    }
}
